﻿using Microsoft.Data.Sqlite;
using SSC.Models;

namespace SSC.Services
{
    public static class Record
    {
        /// <summary>
        /// 插入完整产品记录（含30个槽位停留时间）
        /// </summary>
        public static async Task<bool> InsertProductRecordAsync(ProductModel product, DateTime loadTime, DateTime unloadTime)
        {
            string query = @"
                INSERT INTO tbProductRecord (
                    PartName, LoadDate, UnLoadDate,
                    Column1, Column2, Column3, Column4, Column5, Column6,
                    Column7, Column8, Column9, Column10, Column11, Column12,
                    Column13, Column14, Column15, Column16, Column17, Column18,
                    Column19, Column20, Column21, Column22, Column23, Column24,
                    Column25, Column26, Column27, Column28, Column29, Column30
                ) VALUES (
                    @PartName, @LoadDate, @UnLoadDate,
                    @Column1, @Column2, @Column3, @Column4, @Column5, @Column6,
                    @Column7, @Column8, @Column9, @Column10, @Column11, @Column12,
                    @Column13, @Column14, @Column15, @Column16, @Column17, @Column18,
                    @Column19, @Column20, @Column21, @Column22, @Column23, @Column24,
                    @Column25, @Column26, @Column27, @Column28, @Column29, @Column30
                );";

            var parameters = new List<SqliteParameter>
            {
                new SqliteParameter("@PartName", product.PartName),
                new SqliteParameter("@LoadDate", loadTime),
                new SqliteParameter("@UnLoadDate", unloadTime)
            };

            for (int i = 1; i <= 30; i++)
            {
                int time = product.SlotTimes.TryGetValue(i, out var value) ? value : 0;
                parameters.Add(new SqliteParameter($"@Column{i}", time));
            }

            return await App.DatabaseService.ExecuteCommandAsync(query, parameters.ToArray());
        }
    }
}
